/*
 * @lc app=leetcode.cn id=113 lang=cpp
 *
 * [113] 路径总和 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  vector<vector<int>> ans;

  vector<vector<int>> pathSum(TreeNode* root, int targetSum)
  {
    vector<int> tmp;
    DFS(root, targetSum, 0, tmp);
    return ans;
  }

  void DFS(TreeNode* root, int target, int sum, vector<int>& tmp)
  {
    if (root == nullptr)
      return;
    sum += root->val;
    tmp.push_back(root->val);
    if (root->left == nullptr && root->right == nullptr) {
      if (sum == target)
        ans.push_back(tmp);
    }
    else {
      DFS(root->left, target, sum, tmp);
      DFS(root->right, target, sum, tmp);
    }
    tmp.pop_back();
  }
};
// @lc code=end
